package trainer;

import monster.Monster;
import monster.MonsterException;

import java.util.*;

public class Trainer {

    private String name;

    private Set<Monster> knapsack;

    public Trainer(String name){
        this.name = name;
        knapsack = new HashSet<>();
    }

    public String getName() {
        return name;
    }

    public Set<Monster> getKnapsack(){
        return knapsack;
    }

    /** get monster total*/
    public Integer getMonsterTotal(){
        return knapsack.size();
    }

    /** add monster, return monster location */
    public boolean addMonster(Monster monster) {
        if (knapsack.contains(monster)){
            return false;
        } else {
            return knapsack.add(monster);
        }
    }

    /** remove monster */
    public boolean removeMonster(Monster monster) throws MonsterException {
        return knapsack.remove(monster);
    }

    public boolean hasMonster (Monster monster) {
        if (knapsack.contains(monster)){
            return true;
        }
        return false;
    }

    public Map<String, Integer> countMonstersByType() {
        Iterator iterator = knapsack.iterator();
        Integer fire = 0;
        Integer water = 0;
        Integer electrice = 0;
        while (iterator.hasNext()){
            Monster monster = (Monster) iterator.next();
            switch (monster.getType()) {
                case "Fire":
                    fire ++;
                    break;
                case "Electric":
                    electrice ++;
                    break;
                case "Water":
                    water ++;
                    break;
                default:
                    break;
            }
        }
        Map<String, Integer> expected = new HashMap<>();
        expected.put("Fire", fire);
        expected.put("Electric", electrice);
        expected.put("Water", water);
        return expected;
    }

    @Override
    public String toString(){
        StringBuffer sb = new StringBuffer();
        sb.append("Trainer { 'name' : ").append(name).append(", 'monster total ' :  ").append(knapsack.size()).append(", 'knapsack' : { ");
        Iterator iterator = knapsack.iterator();
        while (iterator.hasNext()){
            Monster monster = (Monster) iterator.next();
            sb.append(monster.toString());
        }
        sb.append(" }");
        return sb.toString();
    }

}
